﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading.Tasks;
using Anywhere.Interface;

namespace MainApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainViewModel _viewModel;

        public MainWindow()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainWindow_Loaded);
            CompositionHelper.PluginChanged += new PluginChangeEventhandle(CompositionHelper_PluginChanged);
            this.MouseLeftButtonDown += new MouseButtonEventHandler(MainWindow_MouseLeftButtonDown);

            _viewModel = new MainViewModel();
            this.DataContext = _viewModel;

            CommandBindings.Add(new CommandBinding(ApplicationCommands.Close,
                new ExecutedRoutedEventHandler(ExecCloseEventHandle),
                new CanExecuteRoutedEventHandler(CanExecCloseEventHandle)));

            lbTypes.SelectionChanged += new SelectionChangedEventHandler(lbTypes_SelectionChanged);

            btnDown.Click += new RoutedEventHandler(btnDown_Click);
            btnUp.Click += new RoutedEventHandler(btnUp_Click);
        }

        void btnUp_Click(object sender, RoutedEventArgs e)
        {
            this.lastRow.Height = new GridLength(100);
            btnUp.Visibility = System.Windows.Visibility.Collapsed;
            btnDown.Visibility = System.Windows.Visibility.Visible;
        }

        void btnDown_Click(object sender, RoutedEventArgs e)
        {
            this.lastRow.Height = new GridLength(25);
            btnDown.Visibility = System.Windows.Visibility.Collapsed;
            btnUp.Visibility = System.Windows.Visibility.Visible;
        }

        void lbTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = lbTypes.SelectedItem as IPlugin;
            if (item == null) return;
            var content = CompositionHelper.Container.GetExportedValueOrDefault<UserControl>(item.ViewContactName);
            if (content != null)
            {
                bContent.Child = content;
            }
        }

        void MainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
            btnTest.Focus();
        }

        void CompositionHelper_PluginChanged()
        {
            _viewModel.ReLoad();
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            CompositionHelper.LoadPlugin();

        }

        void ExecCloseEventHandle(object sender, ExecutedRoutedEventArgs args)
        {
            this.Close();
        }

        void CanExecCloseEventHandle(object sender, CanExecuteRoutedEventArgs args)
        {
            args.CanExecute = true;
        }


    }
}
